package com.fan.springboot1903fanjianghao.controller;

import com.fan.springboot1903fanjianghao.bean.User;
import com.fan.springboot1903fanjianghao.service.impl.UserServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * ZixiaFan
 * 2021/11/23 14:03
 */
@Controller
public class UserController {

    @Autowired
    UserServiceImpl userService;

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @ResponseBody
    @PostMapping("/login.do")
    public User login(@RequestParam("username") String username,
                      @RequestParam("password") String password ,
                      HttpSession session){
        User user= new User();
        user.setPassword(password);
        user.setUsername(username);
        User login = userService.login(user);
        if (login.getStateCode()==0){
            session.setAttribute("user",login);
        }
        return login;
    }

    /**
     * 注册
     * @param username
     * @param password
     * @return
     */
    @ResponseBody
    @PostMapping("/register.do")
    public int register(@RequestParam("username") String username, @RequestParam("password") String password){
        User user= new User();
        user.setPassword(password);
        user.setUsername(username);
        System.out.println(user);
        int res = userService.register(user);
        return res;
    }

    /**
     * 验证用户名是否唯一
     * @return 0 不唯一    1 唯一
     */
    @ResponseBody
    @PostMapping("/UniqueUsername.do")
    public int UniqueUsername(@RequestParam("username") String username){
        int res= userService.UniqueUsername(username);
        return res;
    }

    /**
     * 所有密保问题
     * @param flag
     * @return
     */
    @ResponseBody
    @PostMapping("queryQuestion")
    public List<String>  queryQuestion(@RequestParam("flag") String flag){
        return userService.queryQuestion(flag);
    }

    /**
     * 查询所有，分页显示
     * @param pn
     * @param model
     * @return
     */
    @GetMapping(value = {"/allUser","allUser.html"})
    public String AllUser(@RequestParam(value = "pn", defaultValue = "1")Integer pn, Model model){
        PageHelper.startPage(pn, 5);
        //查询全部
        List<User> users = userService.queryAllUser();
        PageInfo pageInfo = new PageInfo(users, 5);
        model.addAttribute("page",pageInfo);
        return "user/allUser";
    }

    /**
     * 根据id删除某个数据
     * @param id
     * @param pn
     * @param ra
     * @return
     */
    @GetMapping(value = "/user/delUser/{id}")
    public String delUser(@PathVariable("id") Integer id,
                          @RequestParam(value = "pn",defaultValue = "1")Integer pn,
                          RedirectAttributes ra){

        int i = userService.delUser(id);
        ra.addAttribute("pn",pn);
        return "redirect:/allUser";
    }

    @GetMapping(value = {"/QUserByName"})
    public String QUsername(@RequestParam(value = "pn", defaultValue = "1")Integer pn,
                            Model model,
                            @RequestParam("QUsername") String QUsername){
        PageHelper.startPage(pn, 5);
        //根据用户名模糊查询用户
        List<User> users = userService.queryUserByName(QUsername);
        PageInfo pageInfo = new PageInfo(users, 5);
        model.addAttribute("page",pageInfo);
        return "user/allUser";
    }
}
